home *** CD-ROM | disk | FTP | other *** search
/ 1,000+ Great Games / 1_1000 Games.iso / DOSGAMES / BOGGLE.ZIP / SOURCE.ZIP / BOGWIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  12.9 KB  |  419 lines

  1. /*****************************************************************************
  2. * Program:  BOGWIN.CPP
  3. * Purpose:  The guts of the game.  This instantiates everything else.  This
  4. *           can be considered the game object.
  5. *****************************************************************************/
  6. #include  "bogwin.hpp"
  7. #include  "aoptions.hpp"
  8. #include  "options.h"
  9. #include  "about.h"
  10. #include  "about.hpp"
  11.  
  12. /*****************************************************************************
  13. * Function: TBogWindow
  14. * Parms:    title and window ID
  15. * Purpose:  Set up the game by instantiating all other objects
  16. * Returns:  Nothing
  17. *****************************************************************************/
  18. TBogWindow::TBogWindow (const char *title, unsigned long id)
  19.       :  IFrameWindow(title, id, 
  20.                       IFrameWindow::defaultStyle() 
  21.                        | IFrameWindow::minimizedIcon),
  22.          help(IResourceId(HLP_BOGGLEAPP), this),
  23.          menuBar(id, this), 
  24.          Client(FID_CLIENT, this, this),
  25.          gameTitle(0, &Client, &Client),
  26.          myfont(&gameTitle),
  27.          color(IColor::darkGray),
  28.          st_wip(104, &Client, &Client),
  29.          st_list(105, &Client, &Client),
  30.          st_timer(106, &Client, &Client),
  31.          st_score(107, &Client, &Client)
  32. {
  33.    /******************************************************************
  34.    * Add the command handlers to this window
  35.    ******************************************************************/
  36.    addHandler((ICommandHandler *)this);
  37.    addHandler((IPaintHandler *)this);
  38.    addHandler((IMouseClickHandler *)this);
  39.  
  40.    // handle help events
  41.    helpHandler.handleEventsFor(this);
  42.  
  43.    // set the library name and window title for the help window
  44.    help.addLibraries("boggle.hlp")
  45.        .setTitle(WND_MAINWIN)
  46.        .associateWindow(this);
  47.   
  48.    /******************************************************************
  49.    * We will set the range of the scroll bar to equal the number of
  50.    * seconds that the timer will run.  The scroll bar will decrement
  51.    * one position every second.  When there is no more time,  a
  52.    * message box will pop up announcing Game Over.
  53.    ******************************************************************/
  54.    TickDuration = 1500;
  55.  
  56.    menuBar.setAutoDestroyWindow(false);
  57.    GameOn = false;
  58.    bSound = true;
  59.                       
  60.    /***********************************************
  61.    * Attach a client area to the frame.
  62.    ***********************************************/
  63.    this->setClient(&Client);
  64.    Client.setColor(color);
  65.  
  66.    /***************************************
  67.    * Put the title on the client window
  68.    ***************************************/
  69.    unsigned long lineHeight = myfont.maxCharHeight();
  70.    unsigned long alignmentHeight = (lineHeight*3)/2;
  71.   
  72.    myfont.setName("Times New Roman")
  73.          .setPointSize(24)
  74.          .setBold()
  75.          .setItalic();
  76.   
  77.    gameTitle.setAlignment(IStaticText::topLeft)
  78.             .setText("Boggle Game")
  79.             .setFont((myfont));
  80.  
  81.    myfont.setName("Helv")
  82.          .setPointSize(8)
  83.          .setBold(false)
  84.          .setItalic(false);
  85.  
  86.    /**************************************
  87.    * Try for some sound!!
  88.    **************************************/
  89.    try
  90.    {
  91.        psound = new Sound();
  92.    }
  93.    catch ( const IString &msg )
  94.    {
  95.       IMessageBox msgBox(this);
  96.       msgBox.show(msg,
  97.                   IMessageBox::okButton  |
  98.                   IMessageBox::errorIcon |
  99.                   IMessageBox::moveable );
  100.       SetSoundFlag(false);
  101.    }
  102.  
  103.    /**************************************
  104.    * Put the window objects on the screen
  105.    **************************************/
  106.    pscore = new TBogScore(104, &Client, this);
  107.    plist  = new TBogWordList (101, &Client);
  108.    pwip   = new TBogWIP(103, &Client);
  109.    ptimer = new TBogTimer(102, &Client, this, psound); 
  110.    pboard = new TBogBoard(100, &Client, this, pwip, plist, pscore, psound);
  111.  
  112.    /**********************************
  113.    * Put the text controls on the screen
  114.    **********************************/
  115.    Client.setFont(myfont);
  116.  
  117.    st_wip.setText("Word in Progress")
  118.          .setAlignment(IStaticText::bottomLeft)
  119.          .setFont((myfont));
  120.  
  121.    st_list.setText("Word List")
  122.           .setAlignment(IStaticText::bottomLeft)
  123.           .setFont((myfont));
  124.  
  125.    st_timer.setText("Timer")
  126.            .setAlignment(IStaticText::bottomLeft)
  127.            .setFont((myfont));
  128.  
  129.    st_score.setText("Current Score")
  130.            .setAlignment(IStaticText::bottomLeft)
  131.            .setFont((myfont));
  132.  
  133.    /***************************************
  134.    * Position the child windows in canvas
  135.    ***************************************/
  136. //   ISize defaultCell = IMultiCellCanvas::defaultCell();
  137.  
  138.    Client
  139. //      .addToCell(&gameTitle, 3,1,5,1)  //child window,scolumn/row,
  140.       .addToCell(&st_list, 5,1,1,1)      // # of cols and rows
  141.  
  142.       .addToCell(pboard, 2,2,2,2)      //Board control
  143.       .addToCell(plist, 5,2,2,2)       //Word List
  144.  
  145.       .addToCell(&st_wip, 2,4,1,1)     //Word in progress static text
  146.       .addToCell(pwip, 2,5,2,1)        //Word in progress entry field
  147.  
  148.       .addToCell(&st_score, 6,4,1,1)   //Score static text
  149.       .addToCell(pscore, 6,5,1,1)      //Score data field
  150.                                 
  151.       .addToCell(&st_timer, 2,7,1,1)    //Timer static text
  152.       .addToCell(ptimer, 2,8,6,1);      //Timer Slider control
  153.  
  154.    /***************************************
  155.    * Set up the layout of the canvas
  156.    ***************************************/
  157.    Client.setColumnWidth(6, IMultiCellCanvas::defaultCell().width(), false);
  158.  
  159.    Client
  160.       .setRowHeight(1, 15, false)
  161.       .setRowHeight(2, 100, false)
  162.       .setRowHeight(3, 95, false)
  163.       .setRowHeight(4, 15, false)
  164.       .setRowHeight(5, 25, false)
  165.       .setRowHeight(6, 5, false)
  166.       .setRowHeight(7, 15, false)
  167.       .setRowHeight(8, 35, false);
  168.  
  169.    pboard->setMinimumSize(ISize(200, 196));
  170.    plist->setMinimumSize(ISize(200, 96));
  171.  
  172.    /**********************************
  173.    * Size the frame according to the
  174.    * client window.
  175.    **********************************/
  176.    Client.setMinimumSize(ISize(440,330));
  177.    ISize clientSize(Client.minimumSize());
  178.    moveSizeToClient(IRectangle( IPoint(10, 80), clientSize));
  179.  
  180.    //Don't want this to be available until the end of the game
  181.    menuBar.disableItem(IDM_FIND_ALL);
  182.    menuBar.disableItem(IDM_PAUSE_GAME);
  183.    menuBar.disableItem(IDM_RESUME_GAME);
  184.    menuBar.disableItem(IDM_STOP_GAME);
  185. }
  186.  
  187.  
  188. /*****************************************************************************
  189. * Function: ~TBogWindow
  190. * Parms:    none
  191. * Purpose:  delete which thou has newed!
  192. * Returns:  Nothing
  193. *****************************************************************************/
  194. TBogWindow::~TBogWindow ()
  195. {
  196.    if(pscore) 
  197.        delete pscore;
  198.    if(plist) 
  199.        delete plist;
  200.    if(pwip) 
  201.        delete pwip;
  202.    if(ptimer) 
  203.    {
  204.       ptimer->stopTimer();
  205.       delete ptimer;
  206.    } 
  207.    if(pboard) 
  208.        delete pboard;
  209.    if(psound) 
  210.        delete psound;
  211. }
  212.  
  213.  
  214. /*****************************************************************************
  215. * Function: paintWindow
  216. * Parms:    event
  217. * Purpose:  In case I need this during a wm_paint message
  218. * Returns:  Nothing
  219. *****************************************************************************/
  220. Boolean TBogWindow::paintWindow(IPaintEvent &event)
  221. {
  222.    return false;
  223. }
  224.  
  225.  
  226. /*****************************************************************************
  227. * Function: pauseGame
  228. * Parms:    none
  229. * Purpose:  stop the timer temporarily
  230. * Returns:  Nothing
  231. *****************************************************************************/
  232. void TBogWindow::pauseGame()
  233. {
  234.    ptimer->stopTimer();
  235.  
  236.    menuBar.disableItem(IDM_PAUSE_GAME);
  237.    menuBar.enableItem(IDM_RESUME_GAME);
  238.  
  239.    GameOn = false;   
  240. }
  241.  
  242. /*****************************************************************************
  243. * Function: resumeGame
  244. * Parms:    none
  245. * Purpose:  start the timer again
  246. * Returns:  Nothing
  247. *****************************************************************************/
  248. void TBogWindow::resumeGame()
  249. {
  250.    ptimer->startTimer();
  251.  
  252.    menuBar.enableItem(IDM_PAUSE_GAME);
  253.    menuBar.disableItem(IDM_RESUME_GAME);
  254.  
  255.    GameOn = true;   
  256. }
  257.  
  258.  
  259. /*****************************************************************************
  260. * Function: command
  261. * Parms:    event
  262. * Purpose:  process the menu items
  263. * Returns:  false
  264. *****************************************************************************/
  265. Boolean TBogWindow::command(ICommandEvent& event)
  266. {
  267.    Boolean stopProcessingEvent = false;
  268.  
  269.    switch (event.commandId())
  270.    {
  271.       case IDM_GAME_NEW:
  272.          resetGame(event);
  273.          ptimer->startTimer();
  274.  
  275.          break;
  276.       case IDM_GAME_EXIT:
  277.          ptimer->stopTimer();
  278.          WinPostMsg(handle(), WM_CLOSE, 0L, 0L);
  279.          break;
  280.       case IDM_SETTINGS:
  281.          settings();
  282.          break;
  283.  
  284.       case IDM_FIND_ALL:
  285.       {
  286.          WinSetPointer(HWND_DESKTOP, SPTR_WAIT);
  287.  
  288.          //Find all the words that the user should have!
  289.          plist->selectAllWords();
  290.          pboard->FindAll();
  291.  
  292.          WinSetPointer(HWND_DESKTOP, SPTR_ARROW);
  293.          break;
  294.       }
  295.       case IDM_STOP_GAME:
  296.          gameOver();
  297.          break;
  298.       case IDM_PAUSE_GAME:
  299.          pauseGame();
  300.          break;
  301.       case IDM_RESUME_GAME:
  302.          resumeGame();
  303.          break;
  304.       case IDM_HELP_CONTENTS:
  305.          help.show(IHelpWindow::contents);
  306.       return true;
  307.          break;
  308.       case IDM_HELP_ABOUT:
  309.          aboutBox();
  310.          break;
  311.       default:
  312.          break;
  313.    }
  314.    return stopProcessingEvent;
  315. }
  316.  
  317.  
  318. /*****************************************************************************
  319. * Function: mouseClicked
  320. * Parms:    evt
  321. * Purpose:  virtual function to handle the mouse click
  322. * Returns:  false
  323. *****************************************************************************/
  324. Boolean TBogWindow :: mouseClicked(IMouseClickEvent &evt)
  325. {              
  326.    return false;
  327. }
  328.  
  329.  
  330. /*****************************************************************************
  331. * Function: resetGame
  332. * Parms:    event
  333. * Purpose:  start a new game
  334. * Returns:  nothing
  335. *****************************************************************************/
  336. void TBogWindow::resetGame(ICommandEvent& event)
  337. {
  338.    pboard->InitBoard();
  339.    pwip->resetString();                     //Clear out WIP
  340.    plist->clearList();                      //Clear list box
  341.    pscore->clearScore();                    //Reset current score
  342.    ptimer->resetTimer();
  343.  
  344.    menuBar.disableItem(IDM_FIND_ALL);
  345.    menuBar.enableItem(IDM_STOP_GAME);
  346.    menuBar.enableItem(IDM_PAUSE_GAME);
  347.    GameOn = true;   
  348. }
  349.  
  350.  
  351. /*****************************************************************************
  352. * Function: GameOver
  353. * Parms:    none
  354. * Purpose:  end of game processing
  355. * Returns:  nothing
  356. *****************************************************************************/
  357. void TBogWindow::gameOver()
  358. {
  359.    //stop the timer and delete the instance of it
  360.    ptimer->stopTimer();
  361.  
  362. // play an exit wav file!
  363.    psound->playSound("gameover.wav");
  364.  
  365.    IMessageBox msgBox(this);
  366.    msgBox.show("The game is over.  Thank you for playing Boggle!",
  367.                IMessageBox::okButton  |
  368.                IMessageBox::informationIcon |
  369.                IMessageBox::moveable );
  370.  
  371. //adjust the menu options
  372.    menuBar.disableItem(IDM_PAUSE_GAME);
  373.    menuBar.disableItem(IDM_STOP_GAME);
  374.    menuBar.disableItem(IDM_RESUME_GAME);
  375.    menuBar.enableItem(IDM_FIND_ALL);
  376.  
  377.    GameOn = false;   
  378. }
  379.  
  380.  
  381. /*****************************************************************************
  382. * Function: settings
  383. * Parms:    none
  384. * Purpose:  Handle the settings dialog box
  385. * Returns:  nothing
  386. *****************************************************************************/
  387. void TBogWindow::settings()
  388. {
  389.    unsigned short value;                 //Return value from dialog
  390.  
  391. //Create a Text Dialog
  392.    AOptionsDialog * optionsDialog = new AOptionsDialog(this, DLG_BOX, this); 
  393.  
  394.    optionsDialog->showModally();            //Show this Text Dialog as Modal
  395.    value=optionsDialog->result();           //Get result (eg OK or Cancel)
  396.  
  397.    delete optionsDialog;                    //Delete textDialog
  398. }
  399.  
  400. /*****************************************************************************
  401. * Function: aboutBox
  402. * Parms:    none
  403. * Purpose:  Handle the about dialog box
  404. * Returns:  nothing
  405. *****************************************************************************/
  406. void TBogWindow::aboutBox()
  407. {
  408.    unsigned short value;                 //Return value from dialog
  409.  
  410. //Create a Text Dialog
  411.    AboutDialog* ADlg = new AboutDialog(this, dlg_about, this); 
  412.  
  413.    ADlg->showModally();            //Show this Text Dialog as Modal
  414.    value=ADlg->result();           //Get result (eg OK or Cancel)
  415.  
  416.    delete ADlg;                    //Delete textDialog
  417. }
  418.  
  419.